package exercise;

import org.junit.Test;

import java.util.Scanner;

public class MyTest {


    /**
     * 回型数
     * 从键盘输入一个整数 n（1-20），
     * 把数字 1 - n*n  以螺旋形式按顺时针填入其中
     */
    @Test
    public void test1() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入 0 - 20 范围内的整数：");
//        int size = scan.nextInt();
        int size = 7;
        int[][] arr = new int[size][size];
        int startValue = 1;
        int k = 0;
        do {
            if (k % 4 == 0) {
                for (int i = k / 4; i < arr.length - k / 4; i++) {
                    arr[k / 4][i] = startValue++;
                }
                k++;
            } else if (k % 4 == 1) {
                for (int i = k / 4 + 1; i < arr.length - k / 4 - 1; i++) {
                    arr[i][arr.length - 1 - k / 4] = startValue++;
                }
                k++;
            } else if (k % 4 == 2) {
                for (int i = arr.length - 1 - k / 4; i > k / 4 - 1; i--) {
                    arr[arr.length - 1 - k / 4][i] = startValue++;
                }
                k++;
            } else {
                for (int i = arr.length - 2 - k / 4; i > k / 4; i--) {
                    arr[i][k / 4] = startValue++;
                }
                k++;
            }

        } while (k < 2 * size - 1);
        printArray(arr);
    }


    void printArray(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }


    @Test
    public void test2() {
        int[] arr1, arr2;
        arr1 = new int[]{2, 3, 5, 7, 11, 13, 17, 19};
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + "\t");
        }
        System.out.println();

        arr2 = arr1;
        for (int i = 0; i < arr2.length; i += 2) {
            arr2[i] = i;
        }
        for (int num :
                arr1) {
            System.out.print(num + "\t");
        }
        System.out.println();
    }

    /**
     * 数组扩容
     */
    @Test
    public void test3() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        int[] newArr = new int[arr.length << 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        newArr[arr.length] = 10;
        newArr[arr.length + 1] = 20;
        newArr[arr.length + 2] = 30;

        arr = newArr;
        System.out.println(arr.length);
        for (int j : arr) {
            System.out.println(j);
        }
    }

    /**
     * 数组缩容
     */
    @Test
    public void test4() {
        int[] arr = {1, 2, 3, 4, 5, 6, 4, 7};
        // 删除4
        int[] newArr = new int[arr.length - 2];
        int j = 0;
        for (int i = 0; i < arr.length; i++) {

            if (arr[i] != 4) {
                newArr[j++] = arr[i];
            }
        }
        arr = newArr;
        for (int num :
                arr) {
            System.out.print(num + "\t");
        }
    }
}

